package no.awi.wpd.combat.model;

import java.util.*;

import no.awi.wpd.probability.ProbabilityDistribution;
import no.awi.wpd.unit.model.Unit;

public class Combat {

	Unit goodGuys;
	Unit badGuys;
	List<Unit> chargers;

	ContactZone goodGuysContactZone;
	ContactZone badGuysContactZone;
	
	public Unit getGoodGuys() {
		return goodGuys;
	}

	public void setGoodGuys(Unit goodGuys) {
		this.goodGuys = goodGuys;
	}

	public Unit getBadGuys() {
		return badGuys;
	}

	public void setBadGuys(Unit badGuys) {
		this.badGuys = badGuys;
	}

	public ContactZone getGoodGuysContactZone() {
		return goodGuysContactZone;
	}

	public void setGoodGuysContactZone(ContactZone goodGuysInContact) {
		this.goodGuysContactZone = goodGuysInContact;
	}

	public ContactZone getBadGuysContactZone() {
		return badGuysContactZone;
	}

	public void setBadGuysContactZone(ContactZone badGuysInContact) {
		this.badGuysContactZone = badGuysInContact;
	}

	public List<Unit> getChargers() {
		return chargers;
	}

	public void setChargers(Unit... units) {
	    this.chargers = Arrays.asList(units);
	}

	public SortedMap<InitiativeStep, List<AttackStep>> getStrikeOrder() {
        SortedMap<InitiativeStep, List<AttackStep>> steps = new TreeMap<InitiativeStep, List<AttackStep>>();

        List<AttackStep> attackSteps = new ArrayList<AttackStep>();
        attackSteps.addAll(goodGuysContactZone.getAttackSteps());
        attackSteps.addAll(badGuysContactZone.getAttackSteps());

        for (AttackStep attackStep : attackSteps) {
            if (!steps.containsKey(attackStep.getInitiativeStep())) {
                steps.put(attackStep.getInitiativeStep(), new ArrayList<AttackStep>());
            }
            steps.get(attackStep.getInitiativeStep()).add(attackStep);
        }
        return steps;
	}

	public void clearResults() {
		getBadGuysContactZone().setWoundsTaken(ProbabilityDistribution.ZERO());
		getGoodGuysContactZone().setWoundsTaken(ProbabilityDistribution.ZERO());
	}

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((badGuys == null) ? 0 : badGuys.hashCode());
        result = prime * result + ((chargers == null) ? 0 : chargers.hashCode());
        result = prime * result + ((goodGuys == null) ? 0 : goodGuys.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Combat other = (Combat) obj;
        if (badGuys == null) {
            if (other.badGuys != null)
                return false;
        } else if (!badGuys.equals(other.badGuys))
            return false;
        if (chargers == null) {
            if (other.chargers != null)
                return false;
        } else if (!chargers.equals(other.chargers))
            return false;
        if (goodGuys == null) {
            if (other.goodGuys != null)
                return false;
        } else if (!goodGuys.equals(other.goodGuys))
            return false;
        return true;
    }
	
	
	
}
